#include "autoupdatefile.h"
#include <QFile>
#include <QDir>
#include <QTextCodec>

#include "quazip/JlCompress.h"

AutoUpdateFile::AutoUpdateFile()
    : m_NetManagerDownload(nullptr)
    , m_nTime(0)
    , m_FilePath("")
    , m_FileName("")
    , m_DownloadInfo(nullptr)
{

    m_NetManager = new QNetworkAccessManager();
    connect(m_NetManager,SIGNAL(finished(QNetworkReply *)),this, SLOT(finished(QNetworkReply *)));

    qDebug()<<"QSslSocket build:"<<QSslSocket::sslLibraryBuildVersionString();
    qDebug() << "OpenSSL support:" << QSslSocket::supportsSsl();
    qDebug() << "QSslSocket version" << QSslSocket::sslLibraryVersionString();
}
void    AutoUpdateFile::setFilePath(const QString& filePath)
{
    m_FilePath = filePath;
    QDir dir;
    if(!dir.exists(m_FilePath))
    {
        dir.mkdir(m_FilePath);
    }
}
void AutoUpdateFile::requestUrl(QUrl url)
{
     QNetworkRequest quest;
     quest.setUrl(url);
     m_NetManager->get(quest);
}

void AutoUpdateFile::finished(QNetworkReply *reply)
{
    QVariant stateCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    if(stateCode != 200)
    {
        emit replyError(stateCode);
        return;
    }
    QByteArray content = reply->readAll();
    emit replyFinished(content);
    reply->deleteLater();

}

void AutoUpdateFile::startDown(QUrl url)
{
    if(m_NetManagerDownload == nullptr)
    {
        m_NetManagerDownload = new QNetworkAccessManager();
        connect(m_NetManagerDownload, SIGNAL(finished(QNetworkReply *)), this, SLOT(downFinished(QNetworkReply *)));
    }

    QString fileName = m_FilePath + "/" + url.fileName();

    //解决中文路径问题
    QTextCodec *code = QTextCodec::codecForName("GB2312");
    m_FileName = code->fromUnicode(fileName);

    QNetworkRequest quest;
    quest.setUrl(url);
    quest.setHeader(QNetworkRequest::ContentTypeHeader, "application/zip");
    m_DownloadReply = m_NetManagerDownload->get(quest);

    connect(m_DownloadReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downProgress(qint64,qint64)));
    connect(m_DownloadReply, &QNetworkReply::readyRead, this, &AutoUpdateFile::readyRead);

    m_DownloadTime.start();
}

void AutoUpdateFile::downFinished(QNetworkReply *reply)
{
    QVariant stateCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    if(reply->error() == QNetworkReply::NoError)
    {
        emit downloadFinished(stateCode.toInt());
    }
    else {
        QString strError = reply->errorString();
        emit downloadError(strError);
    }
}

void AutoUpdateFile::downProgress(qint64 bytesReceived, qint64 bytesTotal)
{
    if(bytesTotal <= 0) return;

    int nTime = m_DownloadTime.elapsed();

    nTime -= m_nTime;
    double dBytesSpeed = (bytesReceived * 1000.0f) / nTime;

    qint64 leftBytes = bytesTotal - bytesReceived;
    double dLeftTime = (leftBytes*1.0f)/dBytesSpeed;

    if(m_DownloadInfo == nullptr)
    {
        m_DownloadInfo = new DownloadInfo();
    }
    m_DownloadInfo->speed = dBytesSpeed;
    m_DownloadInfo->leftTime = dLeftTime;
    m_DownloadInfo->bytesReceived = bytesReceived;
    m_DownloadInfo->bytesTotal = bytesTotal;

    m_nTime = nTime;

    emit downloadProgress(m_DownloadInfo);
}
void AutoUpdateFile::readyRead()
{
    QFile file(m_FileName);
    if(file.open(QFile::Append))
    {
        file.write(m_DownloadReply->readAll());
    }
    file.close();
}
void AutoUpdateFile::unzipFile()
{
    QStringList lst = JlCompress::extractDir(m_FileName, m_FilePath);
    qDebug() << lst;
}
